use uuid::Uuid;
use crate::ecs::component::Component;

#[derive(Clone)]
pub struct Entity {
    pub id: Uuid,
    pub name: String,
    pub components: Vec<Box<dyn Component>>,
}

impl Entity {
    pub fn new(name: &str) -> Self {
        Self {
            id: Uuid::new_v4(),
            name: name.to_string(),
            components: Vec::new(),
        }
    }

    pub fn add_component<T: Component + 'static>(&mut self, component: T) {
        self.components.push(Box::new(component));
    }

    pub fn get_component<T: Component + 'static>(&self) -> Option<&T> {
        for comp in &self.components {
            if let Some(c) = comp.as_any().downcast_ref::<T>() {
                return Some(c);
            }
        }
        None
    }

    pub fn get_component_mut<T: Component + 'static>(&mut self) -> Option<&mut T> {
        for comp in &mut self.components {
            if let Some(c) = comp.as_any_mut().downcast_mut::<T>() {
                return Some(c);
            }
        }
        None
    }
}